<template>
	<view class="stock-page">
		<view class="card" v-for="(item,index) in dataList" :key="index">
			<view class="stock-type">
				<u-tag v-if="item.inventory_type === '全员盘点'" type="error">全员盘点</u-tag>
				<u-tag v-else>常规盘点</u-tag>
			</view>
			<view class="stock-content">
				<view>
					<u-tag v-if="item.inventory_status == 'NOT_STARTED'"
						type="warning">{{item.inventory_status_name}}</u-tag>
					<u-tag v-else-if="item.inventory_status == 'PARTIAL'"
						type="success">{{item.inventory_status_name}}</u-tag>
					<u-tag v-else-if="item.inventory_status == 'COMPLETED'">{{item.inventory_status_name}}</u-tag>
					<span>{{item.inventory_code}}</span>
				</view>
				<span>{{item.inventory_name}}</span>
				<u-line />
				<view>
					<view>
						<span>创建人</span>
						<span>{{item.creation_user_name}}</span>
						<span>创建时间</span>
						<span>{{item.creation_time}}</span>
					</view>
					<view>
						<span>盘点人</span>
						<span>{{item.inventory_user_name}}</span>
						<span>要求完成时间</span>
						<span>{{item.complete_date}}</span>
					</view>
				</view>
				<u-line />
				<view>
					<up-button type="primary" plain size="normal" @click="handlerDetail(item)">详情</up-button>
					<up-button type="primary" plain size="normal" @click="handlerStock(item)"
						v-show="item.inventory_status !== 'COMPLETED'">盘点</up-button>
					<up-button type="primary" plain size="normal" v-show="item.inventory_status === 'NOT_STARTED'"
						@click="handlerEdit(item)">修改</up-button>
					<up-button type="primary" plain size="normal" v-show="item.inventory_status === 'NOT_STARTED'"
						@click="handlerDelete(item)">删除</up-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
import request, {BASE_URL} from "@/api/request";

	defineProps({
		dataList: {
			type: Array,
			default: () => []
		}
	})

	const handlerStock = (item) => {
		uni.navigateTo({
			url: '/pages/assetStocktaking/stocktaking?item=' + JSON.stringify(item)
		})
	}
	const handlerStock1 = (item) => {
		let main = plus.android.runtimeMainActivity();
		let Intent = plus.android.importClass("android.content.Intent");
		let intent = new Intent(main.getIntent());
		intent.setClassName(main, "com.rfid.lib.views.StockActivity");
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("item", JSON.stringify(item))
    intent.putExtra("tokenKey", "Authorization")
    intent.putExtra("token", request.getToken())
    intent.putExtra("url", BASE_URL)
		main.startActivity(intent);
	}

	const handlerDetail = (item) => {
		uni.navigateTo({
			url: '/pages/assetStocktaking/stocktakingManagement?item=' + JSON.stringify(item)
		})
	}

	const handlerEdit = (item) => {
		uni.navigateTo({
			url: '/pages/stocktaking/add?item=' + JSON.stringify(item)
		})
	}

	const handlerDelete = (item) => {
		uni.showModal({
			title: '删除确认',
			content: `确定删除盘点 ${item.id} 吗？`,
			cancelText: '取消',
			confirmText: '确定',
			success: async (res) => {
				if (res.confirm) {
					try {
						const resp = await request.post('/api/module/Invoke', {
							pageCode: 'assetinventory',
							params: {
								args: [{
										name: 'interfaceCode',
										value: 'Assetinventory.doDeleteOne'
									},
									{
										name: 'id',
										value: item.id
									}
								]
							},
							methodName: 'doDeleteOne'
						})
						if (resp.isSuccess) {
							uni.showToast({
								title: '操作成功',
								icon: 'success',
								complete: () => {
									uni.$emit('refresh-data')
								}
							})
							return
						}
						uni.showToast({
							title: '操作失败',
							icon: 'none'
						})
					} catch (e) {
						console.error(e)
					}
				}
			}
		})
	}
</script>

<style scoped lang="scss">
	.stock-page {
		width: 100%;

		.card {
			margin: 0 20rpx 40rpx 20rpx;
			padding: 20rpx;
			background-color: #fff;
			border-radius: 10rpx;
			position: relative;

			&:first-child {
				margin-top: 40rpx;
			}

			.stock-type {
				display: inline-block;
				position: absolute;
				right: 0;
				top: 0;
				transform: translateY(-30%);
			}

			.stock-content {
				display: flex;
				flex-direction: column;
				font-size: 0.8rem;
				color: #a9a9a9;

				>view:first-child {
					display: flex;
					align-items: center;
				}

				&>view>span {
					color: black;
					font-size: 1rem;
					font-weight: bold;
					margin-left: 20rpx;
				}

				&>span {
					padding-left: 20rpx;
				}

				* {
					padding: 10rpx 0;
				}

				&>view:nth-child(4) {
					display: flex;
					flex-direction: row;

					&>view {
						display: flex;
						flex-direction: column;
						padding-left: 20rpx;
						width: 50%;

						&>span:nth-child(2n+1) {
							color: black;
							font-size: 0.9rem;
						}
					}

					&>view:first-child {
						border-right: 1rpx solid #F0F0F1;
					}
				}

				&>view:last-child {
					margin-top: 10rpx;
					display: flex;
					flex-direction: row;

					&> :deep(.u-button) {
						margin: 0 10rpx;
					}
				}
			}
		}
	}
</style>